<?php 

class Dashboard_IndexController extends Zend_Controller_Action
{
	public function indexAction()
	{
		// Get the entity informations from the request parameters
		$entityClassIdName = $this->_getParam('entity_class');
		$entityIdName = $this->_getParam('entity_idname');

		// If they can't be found, get them from the current logged in user
		if (empty($entityClassIdName) || empty($entityIdName)) {
			$sessAcc = Account_Model_Session::getAccount();
			if ($sessAcc === NULL) {
				$this->_redirect('/login');
			}
			$entityClassIdName = 'user';
			$entityIdName = $sessAcc->getIdName();
		}


		$entObject = Euso_Object::getInstance($entityClassIdName, NULL, $entityIdName);
		if ($entObject === NULL) {
			throw new Zend_Controller_Action_Exception('Object not found', 404);
		}


		$pagename = $this->_getParam('page_name');

		if (empty($pagename)) {
			$pagename ='index';
		} else if ($pagename == 'thread') {
			$pagename = 'threadview';
		}

		// If the page name is empty, show the dashboard that contains small widgets
		if ($pagename !== 'index') {
			if (Kernel::getInstance()->isWidgetActive($pagename) === FALSE) {
				throw new Zend_Controller_Action_Exception('Page not found', 404);
			}
		}


		$this->view->entityClass = $entityClassIdName;
		$this->view->entityId = $entObject->getId();
		$this->view->entityIdName = $entObject->getIdName();
		$this->view->entityNicename = $entObject->getNicename();
		$this->view->entityDisplayName = $entObject->getNicename();

		if (Kernel::getInstance()->isModuleActive('profile')) {
			$this->view->entityAvatarUrl = Profile_Model_Avatar::getFileUrl($entityClassIdName, $entObject->getId(), 96);
		}


		// Prepare params for the widgets
		$this->view->requestParams = $this->getRequest()->getParams();
		$this->view->requestParams['dashboard_path_info'] = '/'. $entityClassIdName .'/'. $entObject->getIdName();
		$this->view->requestParams['dashboard_page_name'] = $pagename;
		$this->view->requestParams['entity_class'] = $entityClassIdName;
		$this->view->requestParams['idname'] = $entObject->getIdName();
		$this->view->requestParams['entity_id'] = $entObject->getId();


		if (Kernel::getInstance()->isModuleActive('profile')) {
			//TODO: all these data should be prepared by profile module
			$profile = Profile_Model_Data::getInstance($entityClassIdName, $entObject->getId());
			if ($profile != NULL) {

				$pData = $profile->getDataArray();

				if (isset($pData['name.first'])) {
					if (empty($pData['name.last']) === FALSE) {
						$this->view->entityDisplayName = $pData['name.first'] .' '. $pData['name.last'];
					} else {
						$this->view->entityDisplayName = $pData['name.first'];
					}
				} else if (empty($pData['name.last']) === FALSE) {
					$this->view->entityDisplayName = $pData['name.last'];
				} else if (empty($pData['name.display']) === FALSE) {
					$this->view->entityDisplayName = $pData['name.display'];
				} else if (empty($pData['name']) === FALSE) {
					$this->view->entityDisplayName = $pData['name'];
				}

				if (isset($pData['status'])) {
					$this->view->entityStatus = $pData['status'];
				} else if (isset($pData['tagline'])) {
					$this->view->entityStatus = $pData['tagline'];
				}
			}
		}

		//TODO: relationship actions
		//CHECK: as widget?
		if ($entityClassIdName === 'group') {
			$sessAcc = Account_Model_Session::getAccount();
			if ($sessAcc != NULL) {
				if (Group_Model_Member::groupHasMember($entObject->getId(), $sessAcc->getId(), TRUE)) {
					$this->view->entityRelationshipUrl = '<a href="'. site_url_base($entObject->getUrl() .'/part') .'">Leave group</a>';
				} else {
					$this->view->entityRelationshipUrl = '<a href="'. site_url_base($entObject->getUrl() .'/join') .'">Join group</a>';
				}
			}
		} else if ($entityClassIdName === 'user') {
			$sessAcc = Account_Model_Session::getAccount();
			if ($sessAcc != NULL && $sessAcc->getId() != $entObject->getId()) {
				if (User_Model_RelationshipMap::has($sessAcc->getId(), $entObject->getId(), 'friend', TRUE)) {
					$this->view->entityRelationshipUrl = '<a href="'. site_url_base($entObject->getUrl() .'/friendremove') .'">Remove as friend</a>';
				} else {
					$this->view->entityRelationshipUrl = '<a href="'. site_url_base($entObject->getUrl() .'/friendadd') .'">Add as friend</a>';
				}
			}
		}


		//TODO: page list
		$pageLists = array();
		$pageLists['user'] = array(
			'profile' => 'Info',
			'wall' => 'Wall',
			'notes' => 'Notes',
		);
		$pageLists['group'] = array(
			'profile' => 'Info',
			'wall' => 'Wall',
			'notes' => 'Notes',
			'forum' => 'Discussions',
		);

		if (is_array($pageLists[$entityClassIdName])) {
			$pageList = $pageLists[$entityClassIdName];
		} else {
			$pageList = array();
		}

		$this->view->pageList = array();
		foreach ($pageList as $pgName => $pgLabel) {
			if (Kernel::getInstance()->isWidgetActive($pgName)) {
				$this->view->pageList[$pgName] = $pgLabel;
			}
		}



		// If the page name is empty, show the dashboard that contains small widgets
		if ($pagename === 'index') {

			$this->view->pageName = NULL;

			//TODO: widget list, widget order and widget titles should be taken from the database with widget name as fallback
			$widgetLists = array();
			$widgetLists['user'] = array(
				'profile' => 'Info',
				'groupsjoined' => 'Groups',
				'activities' => 'Recent Activities',
				'wall' => 'Wall',
			);
			$widgetLists['group'] = array(
				'profile' => 'Info',
				'groupmembers' => 'Members',
				'activities' => 'Recent Activities',
	//			'forum' => 'Forum',
				'wall' => 'Wall',
	//			'shoutbox' => 'Shoutbox',
			);


			$this->view->widgets = array();

			if (is_array($widgetLists[$entityClassIdName])) {
				foreach ($widgetLists[$entityClassIdName] as $wIdName => $wTitle) {
					if (Kernel::getInstance()->isWidgetActive($wIdName)) {
						$widget = Kernel::getInstance()->getWidget($wIdName);
						$widget->setTitle($wTitle);
						$this->view->widgets[] = $widget;
					}
				}
			}

			$this->view->requestParams['widget_display_mode'] = 'small';

		} else {

			$this->view->pageName = $pagename;

			// Set additional flag to tell that the widget will be displayed as stand alone
			// This flag can be used for the widgets to determine how the content will be displayed
			// For example a widget may display the full content in page mode
			$this->view->requestParams['widget_display_mode'] = 'full';

			$widget = Kernel::getInstance()->getWidget($pagename);

			$this->view->pageInfo = $widget;

			$this->view->pageParentName = $widget->getParentName();

		}

		Kernel::getInstance()->setHeadTitle($entObject->getNicename());
	}
}